//
//  YFPhotoActionAlertView.m
//  SquashSpark
//
//  Created by Coollang on 2017/12/27.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFPhotoActionAlertView.h"
#import "UIView+Frame.h"

@interface YFPhotoActionAlertView()<UITableViewDelegate,UITableViewDataSource>

@end
@implementation YFPhotoActionAlertView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ActionCell"];
    self.tableView.rowHeight = 50;
    [self clipsCornerRadius:6 borderWidth:1.5 borderColor:kYellowGreenColor];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.actionItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActionCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ActionCell"];
    }
    cell.textLabel.font = kOpenSansFont(OpenSansRegular, 15.0);
    cell.textLabel.x = 23;
    cell.textLabel.textColor = kTWhiteText;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    YFActionItem *actionItem = self.actionItems[indexPath.row];
    cell.textLabel.text = actionItem.actionName;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YFActionItem *item = self.actionItems[indexPath.row];
    if (item.actionDidSelectBlock) {
        item.actionDidSelectBlock(indexPath, item);
    }
}

+ (instancetype)photoActionAlertView {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}

// 选择图片弹窗的便利方法
- (void)photoActionAlertViewWithTitle:(NSString *)title cameraItem:(ActionDidSelectBlock)cameraAction libraryItem:(ActionDidSelectBlock)libraryAction {
    
    YFActionItem *camera = [YFActionItem actionItemWithName:@"Take A Photo"];
    YFActionItem *library = [YFActionItem actionItemWithName:@"Choose From Library"];
    self.actionItems = @[camera,library];
    camera.actionDidSelectBlock = cameraAction;
    library.actionDidSelectBlock = libraryAction;
    self.titleLabel.text = title;
    [self clipsCornerRadius:6 borderWidth:1.5 borderColor:kYellowGreenColor];
}

@end

@implementation YFActionItem
+ (instancetype)actionItemWithName:(NSString *)actionName {
    YFActionItem *item = [[YFActionItem alloc] init];
    item.actionName = actionName;
    return item;
}
@end

